package com.yilin.tms.data.controller;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.system.system.AppIcon;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.data.service.ISharePostersService;
import com.yilin.tms.core.remote.IOrderRemote;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.core.remote.IVehicleRemote;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/*
 * 获取海报
 * */
@RestController
@RequestMapping("/data/posters/")
public class SharePostersController extends BaseController {

    @Resource
    private IOrderRemote orderRemote;
    @Resource
    private IUserRemote userRemote;
    @Resource
    private IVehicleRemote vehicleRemote;
    @Resource
    private ISharePostersService sharePostersService;


    /**
     * 获取货源
     */
    @PostMapping(value = "goodsSHarePosters")
    @NotNull({"goodsOrderId"})
    public ReturnData<String> goodsSHarePosters(String goodsOrderId) {
        GoodsOrder goodsOrder = ParamUtil.isSuccess(orderRemote.getGoodsOrderById(goodsOrderId));
        ParamUtil.isNull(goodsOrder, "获取货源信息失败");
        ParamUtil.isNull(goodsOrder.getDisplayConsignorArea(), "获取货源发货地城市失败！");
        ParamUtil.isNull(goodsOrder.getDisplayConsigneeArea(), "获取货源收货地城市失败！");
        ParamUtil.isNull(goodsOrder.getGoodsTypeName(), "获取货源货物类型失败！");
        ParamUtil.isNull(goodsOrder.getTransportTypeName(), "获取货源运输类型失败！");
        String image = sharePostersService.goodsSHarePosters(goodsOrder, getLoginUser());
        return jsonView(true, "获取成功", image);
    }

    /**
     * 获取*车辆
     */
    @PostMapping(value = "vehicleSHarePosters")
    @NotNull({"vehicleId"})
    public ReturnData<String> vehicleSHarePosters(String vehicleId) {
        TruckVehicle truckVehicle = ParamUtil.isSuccess(vehicleRemote.getVehicleById(vehicleId));
        ParamUtil.isNull(truckVehicle, "获取抢单信息失败！");
        ParamUtil.isNull(truckVehicle.getTrackNo(), "获取车辆号牌失败！");
        ParamUtil.isNull(truckVehicle.getTrackNo(), "获取车辆型号失败！");
        String image = sharePostersService.vehicleSHarePosters(truckVehicle, getLoginUser());
        return jsonView(true, "获取成功", image);
    }

    /**
     * 获取运单
     */
    @PostMapping(value = "transitSHarePosters")
    @NotNull({"transitOrderId"})
    public ReturnData<String> transitSHarePosters(String transitOrderId) {
        TransitOrder transitOrder = ParamUtil.isSuccess(orderRemote.getTransitOrderById(transitOrderId));
        ParamUtil.isNull(transitOrder, "获取运单信息失败！");
        ParamUtil.isNull(transitOrder.getDisplayConsignorArea(), "获取运单发货地城市失败！");
        ParamUtil.isNull(transitOrder.getDisplayConsigneeArea(), "获取运单收货地城市失败！");
        ParamUtil.isNull(transitOrder.getTransportNo(), "获取运单单号失败！");
        ParamUtil.isNull(transitOrder.getAssignTransportCost(), "获取运费失败！");
        String image = sharePostersService.transitSHarePosters(transitOrder, getLoginUser());
        return jsonView(true, "获取成功", image);
    }

    /**
     * 获取用户海报
     */
    @PostMapping(value = "accountInfoSHarePosters")
    public ReturnData<String> accountInfoSHarePosters(String organizeId) {
        if (organizeId == null) organizeId = getLoginUser().getOrganizeId();
        Organize organize = ParamUtil.isSuccess(userRemote.getOrganizeById(organizeId));
        ParamUtil.isNull(organize, "获取组织信息失败！");
        ParamUtil.isNull(organize.getOrganizeName(), "获取组织名称失败！");
        ParamUtil.isNull(organize.getUserType(), "获取组织类型失败！");
        String image = sharePostersService.accountInfoSHarePosters(organize, getLoginUser());
        return jsonView(true, "获取成功", image);
    }


    /**
     * 生成APP图标
     */
    @PostMapping(value = "generateAppIcon")
    public ReturnData<String> generateAppIcon(AppIcon appIcon) {
        if (appIcon.getLogobase64() == null) return jsonErrView("请选择LOGO图片");
        if (appIcon.getIconRadius() == null) appIcon.setIconRadius(0D);
        if (appIcon.getIconWidth() == null) appIcon.setIconWidth(260D);
        if (appIcon.getIconHeight() == null) appIcon.setIconHeight(260D);
        if (appIcon.getLogoWidth() == null) appIcon.setLogoWidth(200D);
        if (appIcon.getLogoHeight() == null) appIcon.setLogoHeight(200D);
        if (appIcon.getLogoLeft() == null) appIcon.setLogoLeft(30D);
        if (appIcon.getLogoTop() == null) appIcon.setLogoTop(30D);
        String generateAppIcon = sharePostersService.generateAppIcon(appIcon);
        return jsonView(true, "生成APP图标成功", generateAppIcon);
    }
}
